<template>
  <div style="max-width:360px;margin:60px auto">
    <el-card>
      <el-form @submit.native.prevent="submit">
        <el-form-item>
          <el-input v-model="username" placeholder="用户名" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="phone" placeholder="手机号" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="password" type="password" placeholder="密码" />
        </el-form-item>
        <el-button type="primary" @click="submit" style="width:100%">注册</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data () {
    return { username: '', phone: '', password: '' }
  },
  methods: {
    async submit () {
      const { data } = await axios.post('/api/auth/register', { username: this.username, phone: this.phone, password: this.password })
      if (data.code === 1) {
        this.$message.success('注册成功，请登录')
        this.$router.replace('/login')
      } else {
        this.$message.error(data.msg)
      }
    }
  }
}
</script>
